package com.cskaoyan.service;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.order.Comment;
import com.cskaoyan.bean.order.CommentExample;
import com.cskaoyan.bean.order.Order;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.CommentData;
import com.cskaoyan.bean.wx.comment.bo.CommentListBo;
import com.cskaoyan.bean.wx.comment.vo.CommentDataVo;
import com.cskaoyan.bean.wx.comment.vo.CommentNumVo;
import com.cskaoyan.bean.wx.comment.vo.CommentVo;
import com.cskaoyan.bean.wx.comment.vo.UserInfoVo;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kongwo
 * @description
 * @since 2022/05/06 17:28
 */

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 评论页显示
     * @param userId
     * @param valueId
     * @param param
     * @return
     */
    @Override
    public CommentData getCommentList(String userId, String valueId, BaseParam param) {

        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();

        PageHelper.startPage(page, limit);
        List<MarketComment> list = marketCommentMapper.selectCommentList(userId, valueId, sort, order);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CommentData.data(total, pages, limit, page, list);
    }

    /**
     *
     * @param comment
     */
    @Override
    public void deleteComment(MarketComment comment) {

        Integer id = comment.getId();
        marketCommentMapper.updateCommentDeleted(id);
    }

    @Override
    public BaseRespVo getAllCommentList(CommentListBo commentListBo) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andValueIdEqualTo(commentListBo.getValueId());
        criteria.andTypeEqualTo((byte)0);
        PageHelper.startPage(commentListBo.getPage(), commentListBo.getLimit());
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        //执行查询过程中拼接分页信息
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        long total = pageInfo.getTotal();
        int pages = (int) total / commentListBo.getLimit() + 1;

        ArrayList<CommentVo> commentVos = new ArrayList<CommentVo>();
        for (Comment comment : comments) {
            CommentVo commentVo = new CommentVo();
            commentVo.set(comment);
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setAvatarUrl(user.getAvatar());
            userInfoVo.setNickName(user.getNickname());
            commentVo.setUserInfo(userInfoVo);
            commentVos.add(commentVo);
        }
        /*PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        //想要选择三个相关的，可以直接修改这个变成你想要的相关的
        List<TopicComment> topicComments = weChartService.querryTopicComment(valueId);

        PageInfo<TopicComment> info = new PageInfo<>(topicComments);
        TotalPageLimit<List> listTotalPageLimit = new TotalPageLimit<List>((int) info.getTotal(), info.getPages(), baseParam.getLimit(), baseParam.getPage(), topicComments);
        return BaseRespVo.getResult(listTotalPageLimit, "成功", 0);*/
        CommentDataVo commentDataVo = new CommentDataVo();
        commentDataVo.setLimit(commentListBo.getLimit());
        commentDataVo.setPage(commentListBo.getPage());
        commentDataVo.setPages(pages);
        commentDataVo.setTotal(total);
        commentDataVo.setList(commentVos);

        BaseRespVo<CommentDataVo> arrayListBaseRespVo = new BaseRespVo<>();
        arrayListBaseRespVo.setErrno(0);
        arrayListBaseRespVo.setErrmsg("成功");
        arrayListBaseRespVo.setData(commentDataVo);
        return arrayListBaseRespVo;
    }

    @Override
    public BaseRespVo getCountNum(Integer valueId, Integer type) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andTypeEqualTo((byte)type.intValue());
        criteria.andValueIdEqualTo(valueId);
        List<Comment> AllComments = commentMapper.selectByExample(commentExample);
        criteria.andPicUrlsIsNotNull();
        criteria.andPicUrlsNotEqualTo("[]");
        List<Comment> PicComments = commentMapper.selectByExample(commentExample);
        CommentNumVo commentNumVo = new CommentNumVo();
        commentNumVo.setAllCount(AllComments.size());
        commentNumVo.setHasPicCount(PicComments.size());

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setData(commentNumVo);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);

        return baseRespVo;
    }
}
